<?php

function wp_content($type) {
	if ( $type == "index" )
		wp_index();
	if ( $type == "single" )
		wp_single(); 
	if ( $type == "page" )
		wp_page(); 
	if ( $type == "cat" )
		wp_cat(); 
	if ( $type == "archive" )
		wp_archive();
}
/*	
	function BP_layout
	has a list of layouts that can be used with buildpress
	params : 
	$layoutID - int
*/
function BP_layout($layoutID, $type) {
	
	switch($layoutID) {
		case '1':
		?>
		<div id="content">
		<?php wp_content($type); ?>
        </div>
        <?php
		break;
		//25-50-25
		case '3':
		?>
		<div class="yui-ge equalsidebar">
        	<div class="yui-u first">
            	<div class="yui-gd equalsidebar">
                	<div class="yui-u first sidebar" id="sidebar1"><?php get_sidebar_1(); ?></div>
                    <div class="yui-u"><div id="content">
                    <?php wp_content($type); ?>
        		</div></div>
                </div>
            </div>
            <div class="yui-u sidebar" id="sidebar2"><?php get_sidebar_2(); ?></div>
        </div>		
        <?php
		break;
		
		
		//50-25-25
		case '5':
		?>
        <div class="yui-g equalsidebar">
            <div class="yui-u first"><div id="content">
            <?php wp_content($type); ?>
                    </div></div>
            <div class="yui-g">
                <div class="yui-u first sidebar" id="sidebar1"><?php get_sidebar_1(); ?></div>
                <div class="yui-u sidebar" id="sidebar2"><?php get_sidebar_2(); ?></div>
            </div>
        </div>
        <?php
		break;
		
		//25-25-50
		case '6':
		?>
		<div class="yui-g equalsidebar">
        	<div class="yui-g first">
            	<div class="yui-u first sidebar" id="sidebar1"><?php get_sidebar_1(); ?></div>
                <div class="yui-u sidebar" id="sidebar2"><?php get_sidebar_2(); ?></div>
			</div>
            <div class="yui-u"><div id="content"><?php wp_content($type); ?></div></div>
		</div>

        <?php
		break;
		
		//66-33
		case '7':
		?>
        <div class="yui-gc equalsidebar">
            <div class="yui-u first"><div id="content"><?php wp_content($type); ?></div></div>
            <div class="yui-u sidebar" id="sidebar1"><?php get_sidebar_1(); ?></div>
        </div>
        <?php
		break;
		
		//33-66
		case '8':
		?>
        <div class="yui-gd equalsidebar">
        <div class="yui-u first sidebar" id="sidebar1"><?php get_sidebar_1(); ?></div>
        <div class="yui-u" <div id="content"><?php wp_content($type); ?></div></div>
        </div>        
        <?php
		break;
		
		//75-25
		case '9':
		?>
        <div class="yui-ge equalsidebar">
        <div class="yui-u first" <div id="content"><?php wp_content($type); ?></div></div>
        <div class="yui-u" id="sidebar1"><?php get_sidebar_1(); ?></div>
        </div>        
        <?php
		break;
		
		//25-75
		case '10':
		?>
        <div class="yui-gf equalsidebar">
        <div class="yui-u first" id="sidebar1"><?php get_sidebar_1(); ?></div>
        <div class="yui-u" <div id="content"><?php wp_content($type); ?></div></div>
        </div>
        
        <?php		
		break;
		
		//100
		case '1': default:	
		?>
        <div class="yui-g" div id="content"><?php wp_content($type); ?></div></div>            
        <?php
		break;
	}
		
}	


/*	
	function wp_index
	The loop
	params : 
	$customcallback - function
*/
function wp_index( $customcallback = "NULL" ) {
	if (have_posts()) :	while (have_posts()) : the_post(); ?>
		<div class="post" id="post-<?php the_ID(); ?>">        
        <h1 class="posttitle"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
        
         <div class="postmeta">
         <div class="date">
            <div class="month"><?php the_time('M') ?></div>
            <span><?php the_time('d') ?></span>
         </div>
            <p>
            Author : <?php the_author() ?> <?php edit_post_link('Edit', ' | ', ''); ?><br />
            <?php the_tags('Tags: ', ', ', ' | '); ?>Posted in <?php the_category(', ') ?></p>
            <p class="bigcomments"><?php comments_popup_link('<span>No</span> Comments &#187;', '<span>1</span> Comment &#187;', '<span>%</span> Comments &#187;'); ?></p>
        </div>
        
        <div class="entry"><?php the_content('Read more...'); ?></div>
        
        </div>
		<?php endwhile; ?>
        
        <div class="navigation">
			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
		</div>
        
	<?php else : ?>

		<h1 class="center">Not Found</h1>
		<p class="center">Sorry, but you are looking for something that isn't here.</p>

	<?php endif;
}

function wp_cat( $customcallback = "" ) {
	if (have_posts()) :
		$post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
 	  <?php /* If this is a category archive */ if (is_category()) { ?>
		<h2 class="pagetitle">Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>
 	  <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
		<h2 class="pagetitle">Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h2>
 	  <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
 	  <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>
 	  <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>
	  <?php /* If this is an author archive */ } elseif (is_author()) { ?>
		<h2 class="pagetitle">Author Archive</h2>
 	  <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
		<h2 class="pagetitle">Blog Archives</h2>
 	  <?php } ?>
      
		<?php while (have_posts()) : the_post(); ?>
		<div class="post" id="post-<?php the_ID(); ?>">        
        <h1 class="posttitle"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
        
         <div class="postmeta">
         <div class="date">
            <div class="month"><?php the_time('M') ?></div>
            <span><?php the_time('d') ?></span>
         </div>
            <p>
            Author : <?php the_author() ?> <?php edit_post_link('Edit', ' | ', ''); ?><br />
            <?php the_tags('Tags: ', ', ', ' | '); ?>Posted in <?php the_category(', ') ?></p>
            <p class="bigcomments"><?php comments_popup_link('<span>No</span> Comments &#187;', '<span>1</span> Comment &#187;', '<span>%</span> Comments &#187;'); ?></p>
        </div>
        
        
        <div class="entry"><?php the_content('Read more...'); ?></div>
        </div>
		<?php endwhile; ?>

		<div class="navigation">
			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
		</div>
	<?php else :

		if ( is_category() ) { // If this is a category archive
			printf("<h2 class='center'>Sorry, but there aren't any posts in the %s category yet.</h2>", single_cat_title('',false));
		} else if ( is_date() ) { // If this is a date archive
			echo("<h2>Sorry, but there aren't any posts with this date.</h2>");
		} else if ( is_author() ) { // If this is a category archive
			$userdata = get_userdatabylogin(get_query_var('author_name'));
			printf("<h2 class='center'>Sorry, but there aren't any posts by %s yet.</h2>", $userdata->display_name);
		} else {
			echo("<h2 class='center'>No posts found.</h2>");
		}
		get_search_form();

	endif;	
}


function wp_page( $customcallback = "" ) {
	if (have_posts()) : while (have_posts()) : the_post(); ?>
		<div class="post" id="post-<?php the_ID(); ?>">
		<h2><?php the_title(); ?></h2>
			<div class="entry">
				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>

				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

			</div>
		</div>
		<?php endwhile; endif;
}


function wp_single( $customcallback = "" ) {
	if (have_posts()) : while (have_posts()) : the_post(); ?>
    
		<div class="post" id="post-<?php the_ID(); ?>">        
        <h1 class="posttitle"><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
        
         <div class="postmeta">
         <div class="date">
            <div class="month"><?php the_time('M') ?></div>
            <span><?php the_time('d') ?></span>
         </div>
            <p>
            Author : <?php the_author() ?> <?php edit_post_link('Edit', ' | ', ''); ?><br />
            <?php the_tags('Tags: ', ', ', '<br />'); ?>Posted in <?php the_category(', ') ?></p>
        </div>
        <div class="entry"><?php the_content(); ?></div>

<div class="wp-pagenavi">
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?></div>
				<div class="postmetadata">
						This entry was posted
						<?php /* This is commented, because it requires a little adjusting sometimes.
							You'll need to download this plugin, and follow the instructions:
							http://binarybonsai.com/wordpress/time-since/ */
							/* $entry_datetime = abs(strtotime($post->post_date) - (60*120)); echo time_since($entry_datetime); echo ' ago'; */ ?>
						on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?>
						and is filed under <?php the_category(', ') ?>.
						You can follow any responses to this entry through the <?php post_comments_feed_link('RSS 2.0'); ?> feed.

						<?php if ( comments_open() && pings_open() ) {
							// Both Comments and Pings are open ?>
							You can <a href="#respond">leave a response</a>, or <a href="<?php trackback_url(); ?>" rel="trackback">trackback</a> from your own site.

						<?php } elseif ( !comments_open() && pings_open() ) {
							// Only Pings are Open ?>
							Responses are currently closed, but you can <a href="<?php trackback_url(); ?> " rel="trackback">trackback</a> from your own site.

						<?php } elseif ( comments_open() && !pings_open() ) {
							// Comments are open, Pings are not ?>
							You can skip to the end and leave a response. Pinging is currently not allowed.

						<?php } elseif ( !comments_open() && !pings_open() ) {
							// Neither Comments, nor Pings are open ?>
							Both comments and pings are currently closed.

						<?php } edit_post_link('Edit this entry','','.'); ?>

			</div>
            
            </div>
        <div id="comment_template"><?php comments_template(); ?></div>

	<?php endwhile; else: ?>

		<p>Sorry, no posts matched your criteria.</p>

<?php endif;

}

/******************** USD IN THE TEMPLATE *********************/

function get_content($type) {
	BP_Layout( get_attrib("body"), $type );	
}


function get_header_content() {
?>
<h1 id="header_text"><?php bloginfo('name'); ?></h1>

<div class="description"><?php bloginfo('description'); ?></div>
<?php	
}

function get_footer_content() {
?>
<p>
<h4><strong><?php bloginfo('name'); ?></strong> is proudly powered by <a href="http://wordpress.org/">WordPress</a></h4>
</p>
<?php

}

function include_colors_css() {
	echo '<link rel="stylesheet" href="' . BP_REPO_URL . '/colors/default.php?c=' . getColors() . '" type="text/css" />';
}

function get_sidebar_top() {
	if ( check_attrib("bannertop") )
		include( BP_ABSPATH . '/sidebar_top.php' );
}

function get_sidebar_bottom() {
	if ( check_attrib("bannerbottom") )
		include( BP_ABSPATH .'/sidebar_bottom.php' );
}

function get_sidebar_1() {
		include( BP_ABSPATH . '/sidebar_1.php' );

}

function get_sidebar_2() {
		include( BP_ABSPATH . '/sidebar_2.php' );

}

function docstyle() {
	echo get_attrib("docstyle");
}

function getExtraCSS() {
	
	//get attrib stylehsheets
	$stylesheet = get_attrib("stylesheets"); //get all stylesheets i.e header,footer,content,widget
	
	//load every stylesheet link
	foreach ( (array) $stylesheet as $ss ) {
		$ss = explode("-", $ss);
		echo "\n" . '<link rel="stylesheet" href="' . BP_REPO_URL . '/' . $ss[0] . '/' . $ss[1] . '" type="text/css" media="screen" />';
	}
	
	//build css for colors
	echo '<style type="text/css">' . "\n";
	$cssvar = buildCSS(get_attrib("extracss"));	
	foreach ( $cssvar as $k => $v ) {
		if ( $k == "h1" )
			continue;

		$css .= generateTextCSS($k, $v[0], $v[2], $v[1]) . "\n";
	}
	
	echo $css;
	echo "\n" . '</style>';
}

function buildCSS($arrays) {
	$vara = array();
	
	$i = 0;
	foreach ( (array) $arrays as $array ) {
		
		foreach ( (array) $array as $k => $v ) {
			$key = explode("-", $k);
				$vara[$key[1]][] = $v;
		}
	}
	return $vara;
}
